/*
 * Copyright (c) 2009 Roman Savelyev
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#define INCLUDE_E32IMAGEHEADER_IMPLEMENTATION
#include <f32image.h>
#include "Sandbox.h"
#include "Log.h"

CSandbox* CSandbox::NewL(TUint32 aImageSize)
    {
    LOG2(_L("-->CSandbox::NewL(%d)"), aImageSize);
    CSandbox* self = new(ELeave) CSandbox(aImageSize);
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop(self);
    LOG(_L("<--CSandbox::NewL"))
    return self;
    }

CSandbox::CSandbox(TUint32 aImageSize) :
    iImageSize(aImageSize),
    iAdditionalStack(NULL)
    {}

void CSandbox::ConstructL()
    {
    LOG(_L("-->CSandbox::ConstructL"));
    User::LeaveIfError(iImage.CreateLocalCode(iImageSize, iImageSize));
    LOG(_L("<--CSandbox::ConstructL"));
    }

CSandbox::~CSandbox()
    {
    LOG(_L("-->CSandbox::~CSandbox"));
    iImage.Close();
    LOG(_L("<--CSandbox::~CSandbox"));
    }

TUint32 CSandbox::ImageSize() const
    {
    return iImageSize;
    }

E32ImageHeaderV& CSandbox::E32ImageHeader()
    {
    return *(E32ImageHeaderV*)iImage.Base();
    }

const E32ImageHeaderV& CSandbox::E32ImageHeader() const
    {
    return *(const E32ImageHeaderV*)iImage.Base();
    }

TPtr8 CSandbox::E32ImageContent()
    {
    TUint32 contentSize = iImageSize - sizeof(E32ImageHeaderV);
    return TPtr8((TUint8*)(&E32ImageHeader() + 1), contentSize, contentSize);
    }

TPtrC8 CSandbox::E32ImageContent() const
    {
    return TPtrC8((TUint8*)(&E32ImageHeader() + 1), iImageSize - sizeof(E32ImageHeaderV));
    }

void CSandbox::ValidateHeaderL()
    {
    LOG(_L("-->CSandbox::ValidateHeaderL"));
    E32ImageHeaderV& header = E32ImageHeader();
    User::LeaveIfError(header.ValidateWholeImage(&header, iImageSize));
    LOG(_L("<--CSandbox::ValidateHeaderL"));
    }

void CSandbox::PrepareL()
    {
    LOG(_L("-->CSandbox::PrepareL"));

    LOG(_L("<--CSandbox::PrepareL"));
    }

void CSandbox::StartL()
    {
    LOG(_L("-->CSandbox::StartL"));
    LOG(_L("<--CSandbox::StartL"));
    }
